<?php
/**
 * Created by PhpStorm.
 * User: 申霖
 * Date: 2019/8/27 0027
 * Time: 下午 7:37
 */

class User extends CI_Controller
{
    /**
     * 列表页
     * @author 申霖
     * @time 2019/8/29 0029 下午 7:46
     */
    public function home()
    {
        $this->load->library('pagination');

        $baseUrl      = $this->config->item('base_url');
        $number       = 3;
        $per_page     = $this->input->get('per_page');
        $current_page = intval($per_page) == 0 ? 1 : intval($per_page);
        $this->db->limit($number, ($current_page - 1) * $number);
        $this->db->where('display', 1);
        $this->db->order_by("id desc");
        $data['data']       = $this->db->get("user")->result_array();
        $config['base_url'] = $baseUrl . '/user.html';
        $this->db->where('display', 1);
        $count                          = $this->db->count_all("user");
        $config['total_rows']           = $count;
        $config['per_page']             = $number;
        $config['num_links']            = $number;
        $config['next_link']            = '下一页';
        $config['prev_link']            = '上一页';
        $config['query_string_segment'] = 'per_page';
        $config['use_page_numbers']     = TRUE;
        $config['page_query_string']    = TRUE;
        $config['full_tag_open']        = '<div class="page"><ul>';
        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $this->load->view('user/index', $data);
    }

    /**
     * 添加
     * @author 申霖
     * @time 2019/8/29 0029 下午 7:47
     */
    public function add()
    {
        $data['id'] = $data['name'] = $data['type'] = '';
        $this->load->view('user/update', $data);
    }

    /**
     * 数据操作
     * @author 申霖
     * @time 2019/8/29 0029 下午 7:59
     */
    public function data()
    {
        $id           = $this->input->post("id");
        $data['name'] = $this->input->post("name");
        $data['type'] = $this->input->post("type");
        $pass         = $this->input->post("pass");
        if ($pass) {
            $data['pass'] = md5($pass);
        }
        if ($id) {
            $data['uptime'] = time();
            $this->db->where('id', $id);
            $this->db->update("user", $data);
        } else {
            $data['display'] = 1;
            $data['intime']  = time();
            $this->db->insert("user", $data);
        }
        echo json_encode(array('code' => 200, 'msg' => '操作成功'));
    }

    /**
     * 删除
     * @author 申霖
     * @time 2019/8/29 0029 下午 8:11
     */
    public function del()
    {
        $id = $this->input->post("id");
        if (!$id) {
            echo "缺少ID参数";
        }
        $this->db->where('id', $id);
        $this->db->update('user', array("display" => 2));
        echo json_encode(array('code' => 200, 'msg' => '删除成功'));
    }

    /**
     * 修改
     * @author 申霖
     * @time 2019/8/29 0029 下午 8:23
     */
    public function update()
    {
        $id = $this->input->get("id");
        if (!$id) {
            echo "缺失参数";
            die;
        }
        $this->db->where("id", $id);
        $this->db->limit(1);
        $data = $this->db->get("user")->result_array();
        if (!$data) {
            echo "数据获取失败";
            die;
        }
        $data['id']   = $data[0]['id'];
        $data['name'] = $data[0]['name'];
        $data['type'] = $data[0]['type'];
        $this->load->view('user/update', $data);
    }
}
